package com.lwq.controller.system;

import com.lwq.annotation.PermissionController;
import com.lwq.annotation.PermissionInterface;
import com.lwq.annotation.RepeatSubmit;
import com.lwq.controller.BaseController;
import com.lwq.exception.FieldException;
import com.lwq.model.Dto;
import com.lwq.model.Dtos;
import com.lwq.model.Result;
import com.lwq.model.system.SysDict;
import com.lwq.model.system.SysDictDetail;
import com.lwq.service.system.DictService;
import com.lwq.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/6/10
 */
@PermissionController(type = "system")
@RestController
public class DictController extends BaseController {

    @Autowired
    private DictService service;

    @GetMapping("dict")
    @PermissionInterface(name="查询字典", code="dict.query")
    public TableDataInfo queryList(){
        Dto dto = Dtos.newDto(request);
        startPage();
        return getDataTable(service.queryList(dto));
    }

    @GetMapping("dict/cache")
    @PermissionInterface(name="查询字典", code="dict.query")
    public Object queryDictCache(){
        return service.queryListCache();
    }

    @GetMapping("dict/detail")
    @PermissionInterface(name="查询字典", code="dict.query")
    public TableDataInfo queryDetailList(){
        Dto dto = Dtos.newDto(request);
        if(dto.get(SysDictDetail.DICT_ID) == null){
            throw new FieldException("字典ID不可为空");
        }
        startPage();
        return getDataTable(service.queryDictList(dto));
    }

    @GetMapping("dict/refresh")
    @PermissionInterface(name="修改字典", code="dict.update")
    public Result refresh(){
        return Result.ok(service.refresh());
    }

    @PostMapping("dict")
    @RepeatSubmit
    @PermissionInterface(name="新增字典", code="dict.add")
    public Result add(@RequestBody @Valid SysDict dict){
        service.addDict(dict);
        return Result.ok();
    }

    @PostMapping("dict/detail")
    @RepeatSubmit
    @PermissionInterface(name="新增字典", code="dict.add")
    public Result addDetail(@RequestBody @Valid SysDictDetail dictDetail){
        service.addDictDetail(dictDetail);
        return Result.ok();
    }

    @PutMapping("dict")
    @RepeatSubmit
    @PermissionInterface(name="修改字典", code="dict.update")
    public Result update(@RequestBody @Valid SysDict dict){
        service.editDict(dict);
        return Result.ok();
    }

    @PutMapping("dict/detail")
    @RepeatSubmit
    @PermissionInterface(name="修改字典", code="dict.update")
    public Result update(@RequestBody @Valid SysDictDetail dictDetail){
        service.editDictDetail(dictDetail);
        return Result.ok();
    }

    @DeleteMapping("dict/{id}")
    @PermissionInterface(name="删除字典", code="dict.remove")
    public Result remove(@PathVariable Long id){
        service.delDict(id);
        return Result.ok();
    }

    @DeleteMapping("dict/detail/{id}")
    @PermissionInterface(name="删除字典", code="dict.remove")
    public Result removeDetail(@PathVariable Long id){
        service.delDictDetail(id);
        return Result.ok();
    }
}
